import java.util.ArrayList;
import java.util.List;

/**
 * 1759. 统计同构子字符串的数目
 * https://leetcode-cn.com/problems/count-number-of-homogenous-substrings/
 */
public class Solutions_1759 {
    public static void main(String[] args) {
        String s1 = "abbcccaa";  // output: 13
        String s2 = "xy";  // output: 2
        String s3 = "zzzzz";  // output: 15

        List<String> inputs = new ArrayList<>();
        inputs.add(s1);
        inputs.add(s2);
        inputs.add(s3);

        for (String input : inputs) {
            int result = countHomogenous(input);
            System.out.println(result);
        }
    }

    /**
     * 解题思路：高斯求和的应用
     */
    public static int countHomogenous(String s) {
        char[] arr = s.toCharArray();
        int mod = 1_000_000_007;
        // 使用 long 来记录
        long res = 0;
        long cnt = 1;

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] == arr[i - 1]) {
                cnt ++;
            } else {
                res += (1 + cnt) * cnt / 2;
                res %= mod;
                cnt = 1;
            }
        }
        res += (1 + cnt) * cnt / 2;
        res %= mod;
        return (int) res;
    }
}
